package Work2;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class DeserializeUsers {
    public static void main(String[] args) {
        try {
            // 获取当前目录路径
            Path currentDir = Paths.get("");
            // 使用流查找当前目录下的所有.obj文件
            try (Stream<Path> paths = Files.find(currentDir, 1, (path, attrs) -> path.toString().endsWith(".obj"))) {
                paths.forEach(path -> {
                    try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path.toFile()))) {
                        // 反序列化User对象
                        User user = (User) ois.readObject();
                        // 输出User对象信息
                        System.out.println(user);
                    } catch (IOException | ClassNotFoundException e) {
                        System.err.println("反序列化文件 " + path.getFileName() + " 时出错: " + e.getMessage());
                    }
                });
            }
        } catch (IOException e) {
            System.err.println("查找文件时出错: " + e.getMessage());
        }
    }
}